/**
 * Package:com.goldcitynet.service.impl;
 * Id: SceneServiceImpl.java,$v 1.3 2008/05/05 10:47:35 $yangchuan Exp 
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.engine.service.impl;

import java.util.List;

import com.afteryuan.engine.model.Scene;
import com.afteryuan.engine.service.SceneService;
import com.afteryuan.dao.HibernateEntityDao;
import com.afteryuan.util.PaginationBean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * SceneServiceImpl
 * <p/>
 * <p><a href="SceneServiceImpl.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.1.1.1 $
 */
public class SceneServiceImpl extends HibernateEntityDao<Scene> implements SceneService {

    private static Log log = LogFactory.getLog(SceneServiceImpl.class);

    //获得指定的场景配置
    public Scene getSceneByKeyWord(String keyWord, String order, Object... values) {
        List<Scene> scenes = null;
        StringBuffer hql = new StringBuffer("from As3Scene sc where 1=1 ");
        if (order != null && !order.equals("")) {
            hql.append(" order by sc." + order);
        }
        if (values.length > 0 && values[0] instanceof PaginationBean)
            scenes = createList(hql.toString(), values[0]);
        else
            scenes = createList(hql.toString());

        return (scenes == null || scenes.size() <= 0) ? null : scenes.get(0);
    }

    public List<Scene> list(String name, String type) {
        StringBuffer hql = new StringBuffer("from Scene where 1=1");
        if (name != null && !name.equals("")) {
            hql.append(" and name like '%" + type + "%'");
        }
        if (type != null && !type.equals("")) {
            hql.append(" and type=" + type);
        }
        return createList(hql.toString());
    }
     public List<Scene> list(Scene scene, Object... values) {
        StringBuffer hql = new StringBuffer("from Scene s where 1=1");
        if (scene != null && !scene.equals("")) {
            if (scene.getStatus() != null && !scene.getStatus().equals("")) {
                hql.append(" and s.status = " + scene.getStatus());
            }
             if (scene.getType() != null && !scene.getType().equals("")) {
                hql.append(" and s.type = " + scene.getType());
            }
//            if (scene.getUpLevelScene() != null && !scene.getUpLevelScene().equals("")) {
//                hql.append(" and s.upLevelScene.id ='").append(scene.getUpLevelScene().getId()).append("'");
//            }
        }
        hql.append(" order by createTime desc");
        log.debug(hql);
        if (values.length > 0 && values[0] instanceof PaginationBean) return createList(hql.toString(), values[0]);
        return createList(hql.toString());
    }

    public void clearTransArea(String sceneId) throws Exception {
        String hql1 = "update As3Scene set east=null where east='" + sceneId + "'";
        createQuery(hql1).executeUpdate();
        String hql2 = "update As3Scene set west=null where west='" + sceneId + "'";
        createQuery(hql2).executeUpdate();
        String hql3 = "update As3Scene set north=null where north='" + sceneId + "'";
        createQuery(hql3).executeUpdate();
        String hql4 = "update As3Scene set south=null where south='" + sceneId + "'";
        createQuery(hql4).executeUpdate();
    }

    //根据上级场景ID和坐标dircx、dircy获得场景对象
    public Scene getScene(String upSceneId, Integer dircx, Integer dircy, String order, Object... values) {
        List<Scene> scenes = null;
        StringBuffer hql = new StringBuffer("from As3Scene sc where 1=1 ");
//        if (upSceneId != null && !upSceneId.equals("")) {
//            hql.append(" and sc.upLevelScene.id='" + upSceneId + "'");
//        }
//        if (dircx != null && !dircx.equals("")) {
//            hql.append(" and sc.dircX=" + dircx + " ");
//        }
//        if (dircy != null && !dircy.equals("")) {
//            hql.append(" and sc.dircY=" + dircy + " ");
//        }
        if (order != null && !order.equals("")) {
            hql.append(" order by sc." + order);
        }
        log.debug(hql);
            
        if (values.length > 0 && values[0] instanceof PaginationBean)
            scenes = createList(hql.toString(), values[0]);
        else
            scenes = createList(hql.toString());

        return (scenes == null || scenes.size() <= 0) ? null : scenes.get(0);
    }

    public Scene getScene(Integer dircx, Integer dircy) {
        return getScene(null, dircx, dircy, null);
    }

    public Scene getNeighbor(Scene scene, Integer direction) {
//        if (scene.getDircX() != null && scene.getDircY() != null) {
//            int x = scene.getDircX();
//            int y = scene.getDircY();
//            if (Scene.DIRECT_EAST.equals(direction)) x++;
//            if (Scene.DIRECT_NORTH.equals(direction)) y--;
//            if (Scene.DIRECT_WEST.equals(direction)) x--;
//            if (Scene.DIRECT_SOUTH.equals(direction)) y++;
//            log.debug(scene.getDircX()+":"+scene.getDircY());
//            log.debug(x+"="+y);
//            return getScene(x, y);
//        }
        return null;
    }

//    public List<Scene> listByFrequency(Integer length) {
//        String hql="from As3Scene order by frequency desc";
//        return createList(hql,new PaginationBean(length));
//    }


}